<?php
require_once 'Connection.php';
require_once 'Model.php';

// id, name, content, parentid
class Topic extends Model {
	
	public function __construct() {	
	    $this->_idFieldName = 'id';
	    $this->_entityName = 'topic';
    	$this->_db = Connection::getInstance();
	}
	
	public function getChilds() {
		$id = $this->getId();
		$query = ($id)? 
			"select p.id, p.name, count(c.topic_id) as child_count from topic p left join topic c on p.id=c.topic_id where p.topic_id=$id group by p.id": 
			"select p.id, p.name, count(c.topic_id) as child_count from topic p left join topic c on p.id=c.topic_id where p.topic_id is null group by p.id";
		$result = $this->_db->executeQuery($query);
		$items = array();
		if ($result) {
			while ($data = $result->getRow()) {
				$item = $this->createEntity($data);
				array_push($items, $item);
			}
		}
		return $items;
	}

}
